﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Collections;

namespace QunFaExpert
{
    public abstract class IReader
    {
        public IReader(string filename)     //   创建实例的时候就一定要文件名 
        {
            _filename = filename;
        }
        protected string _filename = " ";                         //   输入文件名 
        /// <summary>
        /// Get at list from file
        /// </summary>
        /// <param name="atList"></param>
        /// <returns></returns>
        public abstract List<string> GetAtList(List<string> atList);
    }

    ///   <summary> 
    ///   文本文件类 
    ///   </summary> 
    public class TxtReader : IReader
    {
        public TxtReader(string filename) : base(filename) { }
        public override List<string> GetAtList(List<string> atList)
        {
            bool firstline = true;
            using (var fs = new StreamReader(_filename))
            {
                while (!fs.EndOfStream)
                {
                    var line = fs.ReadLine();
                    char[] seperator = new char[] { ',', '\t' };
                    var linedetail = line.Split(seperator);
                    if ( firstline )
                    {
                        firstline = false;
                    }
                    else
                    {
                        atList.Add(linedetail[0]);
                    }
                }
            }
            return atList;
        }
    }

    /// <summary>
    /// Excel file reader
    /// </summary>
    public class XlsReader : IReader
    {
        private string _outtxtfile = @"c:\temp\temp";   //   暂存盘文件 
        public XlsReader(string filename) : base(filename) { }
        public override List<string> GetAtList(List<string> atList)
        {
            #region   将EXCEL文件另存为TXT文件
            Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            xApp.Visible = false;
            object vk_false = false;
            object vk_readonly = true;

            //   打开文件 
            Microsoft.Office.Interop.Excel.Workbook xBook = null;
            try
            {
                xBook = xApp.Workbooks.Open(_filename,
                Missing.Value, vk_readonly, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            }
            catch
            {
                return atList;
            }

            object vk_saveformat = Microsoft.Office.Interop.Excel.XlFileFormat.xlUnicodeText;
            int iSheets = 0;
            ArrayList sTemps = new ArrayList();
            try
            {
                foreach (Microsoft.Office.Interop.Excel.Worksheet xSheet in xBook.Sheets)
                {
                    string sFilename = _outtxtfile + iSheets + ".txt";
                    sTemps.Add(sFilename);
                    if (File.Exists(sFilename))
                        File.Delete(sFilename);

                    xSheet._SaveAs(sFilename, vk_saveformat,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                    iSheets++;
                }
            }
            catch
            {   //MessageBox.Show( "Error   :   "+ex.ToString());} 
                return atList;
            }
            finally
            {   //Don 't   forget   to   close   everything   up... 
                if (xBook != null)
                {
                    xBook.Close(vk_false, Missing.Value, Missing.Value);
                }
                xApp.Quit();
            }
            #endregion

            try
            {
                foreach (string sFile in sTemps)   //   依次打个各表单保存的文本文件 
                {
                    var txtreader = ReaderFactory.GetInstance(sFile);
                    atList.AddRange(txtreader.GetAtList(atList));
                    File.Delete(sFile);
                }
            }
            catch(Exception ex)
            {
                return atList;
            }

            return atList;
        }
    }
    public class ReaderFactory
    {
        public static IReader GetInstance(string filename)
        {
            Dictionary<string,IReader> readers = new Dictionary<string,IReader>()
            { 
                {"txt",new TxtReader(filename)},
                {"csv",new TxtReader(filename)},
                {"xls", new XlsReader(filename)},
                {"xlsx", new XlsReader(filename)}
            };
            string extension = filename.Substring(filename.LastIndexOf('.') + 1).ToLower();
            return readers[extension];
        }
    }
}
